package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.CustomerHelper;
import com.Helper.FlightHelper;
import com.Helper.ReservationHelper;

public class BackButtonServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	
	String username = null;
	int point = -1;
	int numRes = -1;
	int numPointToClaim = -1;
	
	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
    //@assignable flightID,username;
    //@ensures 	req.setAttribute("flightID", flightID) && req.setAttribute("username", username) && req.getRequestDispatcher("reservation/reserveFlight.jsp").forward(req, resp);	
	//@signals_only RuntimeException, IOException,ServletException;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();

		username = req.getParameter("username");
		
		System.out.println(username);		
		try
		{
			FlightHelper.Setup(connection);
			CustomerHelper.Setup(connection);
			ReservationHelper.Setup(connection, username);
			ReservationHelper.UpdateResStatus(username);
			
			if (username == null || username.isEmpty() || username.equals("null"))
			{
				req.getRequestDispatcher("index.html").forward(req, resp);
			}
			else
			{		
				point = CustomerHelper.GetSelectedCustomer(username).point;
				numRes = ReservationHelper.GetReservationList().size();
				numPointToClaim = ReservationHelper.GetNumPointsToClaim();
				
				req.setAttribute("username", username);
				req.setAttribute("point", point);
				req.setAttribute("numRes", numRes);
				req.setAttribute("numPointToClaim", numPointToClaim);
				req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);
			}
		}
		catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
	}

}

